/* Theme Variables */
:root {
    /* Light Theme Colors */
    --bg-color: #f8f9fa;
    --text-color: #212529;
    --border-color: rgba(0, 0, 0, 0.1);
    --card-bg: #ffffff;
    --card-header-bg: #f8f9fa;
    --hover-bg: #e9ecef;
    --selected-bg: #cfe2ff;
    --selected-border: #9ec5fe;
    --shadow-color: rgba(0, 0, 0, 0.075);
    --delete-zone-bg: rgba(220, 53, 69, 0.1);
    --delete-zone-border: #dc3545;
    --delete-zone-shadow: rgba(220, 53, 69, 0.2);
    --primary-color: #007bff;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --warning-color: #ffc107;
    --info-color: #17a2b8;
}

[data-bs-theme="dark"] {
    /* Dark Theme Colors */
    --bg-color: #212529;
    --text-color: #f8f9fa;
    --border-color: rgba(255, 255, 255, 0.1);
    --card-bg: #343a40;
    --card-header-bg: #2c3136;
    --hover-bg: #495057;
    --selected-bg: #3e4c58;
    --selected-border: #495057;
    --shadow-color: rgba(0, 0, 0, 0.2);
    --delete-zone-bg: rgba(220, 53, 69, 0.2);
    --delete-zone-border: #dc3545;
    --delete-zone-shadow: rgba(220, 53, 69, 0.3);
    --primary-color: #0d6efd;
    --success-color: #2ecc71;
    --danger-color: #e74c3c;
    --warning-color: #f1c40f;
    --info-color: #1abc9c;
}

/* Theme Transitions */
*, *::before, *::after {
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease;
}

/* Base Styles */
html, body {
    height: 100%;
    margin: 0;
    overflow: hidden;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Layout */
.container-fluid {
    flex: 1;
    padding: 0;
    overflow: hidden;
}

.row {
    margin: 0;
    height: calc(100vh - 108px);
    overflow: hidden;
}

/* Common Utility Classes */
.no-spinners::-webkit-inner-spin-button,
.no-spinners::-webkit-outer-spin-button { 
    -webkit-appearance: none;
    margin: 0;
}

.no-spinners {
    -moz-appearance: textfield;
}

.clean-focus:focus {
    background-color: transparent;
    color: inherit;
    border-color: var(--border-color);
    box-shadow: none;
}

.clean-focus:focus-visible {
    outline: none;
}

/* Navbar Styles */
.navbar {
    background-color: var(--card-bg) !important;
    border-bottom: 1px solid var(--border-color);
    box-shadow: 0 1px 3px var(--shadow-color);
    height: 56px;
    flex-shrink: 0;
    padding: 0.5rem 1rem;
    z-index: 1030;
}

.navbar-brand {
    display: flex;
    align-items: center;
    font-size: 1.25rem;
    color: var(--text-color);
}

.navbar-brand i {
    font-size: 1.4rem;
    color: var(--text-color);
}

.navbar-brand .brand-text {
    font-weight: 500;
}

.navbar-brand small {
    font-size: 0.875rem;
    opacity: 0.75;
}

/* Navbar Controls */
.navbar .btn-group {
    display: flex;
    gap: 0.25rem;
    transition: all 0.2s ease-in-out;
}

.navbar .btn-group:hover {
    transform: translateY(-1px);
}

.navbar .btn {
    padding: 0.375rem 0.75rem;
    font-size: 0.9rem;
}

.navbar .btn i {
    font-size: 1rem;
}

.navbar .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Navbar Button Variants */
.navbar .btn-outline-secondary {
    border-color: var(--border-color);
    color: var(--text-color);
}

.navbar .btn-outline-secondary:hover {
    background-color: var(--hover-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

.navbar .btn-outline-dark {
    border-color: var(--border-color);
    color: var(--text-color);
}

.navbar .btn-outline-dark:hover {
    background-color: var(--card-bg);
    color: var(--text-color);
}

.navbar .btn-outline-danger {
    border-color: var(--danger-color);
    color: var(--danger-color);
}

.navbar .btn-outline-danger:hover {
    background-color: var(--danger-color);
    color: var(--text-color);
}

/* Navbar Inputs */
.navbar input[type="text"],
.navbar input[type="number"] {
    font-size: 0.9rem;
    font-family: monospace;
}

.key-label {
    pointer-events: none;
    user-select: none;
}

.key-label:hover,
.key-label:active,
.key-label:focus {
    background-color: transparent;
    color: var(--primary-color);
    border-color: var(--primary-color);
}

/* Canvas Styles */
.col-md-9, .col-md-3 {
    height: 100%;
    padding: 0.5rem;
}

.col-md-3 {
    overflow-y: auto;
    overflow-x: auto;
    min-width: 300px;
    height: calc(100vh - 96px);
    padding-bottom: 1rem;
}

#canvasContainer {
    width: 100%;
    height: 100%;
    background-color: var(--card-bg);
    position: relative;
    overflow: visible;
    border-radius: 0.375rem;
    box-shadow: 0 0 0.125rem var(--shadow-color);
    border: 1px solid var(--border-color);
    padding: 0.5rem;
}

#canvasContainer.drag-over {
    border: 2px dashed var(--primary-color) !important;
    background-color: rgba(0, 123, 255, 0.1);
}

#mappingCanvas {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

/* Mapping Point Styles */
.mapping-point {
    cursor: move;
    user-select: none;
}

.mapping-point:hover {
    filter: brightness(1.2);
}

.mapping-point[data-type="KMT_CLICK"] { fill: var(--primary-color); }
.mapping-point[data-type="KMT_CLICK_TWICE"] { fill: var(--success-color); }
.mapping-point[data-type="KMT_CLICK_MULTI"] { fill: var(--danger-color); }
.mapping-point[data-type="KMT_DRAG"] { fill: var(--warning-color); }
.mapping-point[data-type="KMT_STEER_WHEEL"] { fill: var(--info-color); }

/* Mapping Type Icons */
.mapping-type-item[data-type="KMT_CLICK"] i,
.node-list-item[data-type="KMT_CLICK"] i { color: var(--primary-color); }
.mapping-type-item[data-type="KMT_CLICK_TWICE"] i,
.node-list-item[data-type="KMT_CLICK_TWICE"] i { color: var(--success-color); }
.mapping-type-item[data-type="KMT_CLICK_MULTI"] i,
.node-list-item[data-type="KMT_CLICK_MULTI"] i { color: var(--danger-color); }
.mapping-type-item[data-type="KMT_DRAG"] i,
.node-list-item[data-type="KMT_DRAG"] i { color: var(--warning-color); }
.mapping-type-item[data-type="KMT_STEER_WHEEL"] i,
.node-list-item[data-type="KMT_STEER_WHEEL"] i { color: var(--info-color); }

/* Card Styles */
.card {
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    box-shadow: 0 0.125rem 0.25rem var(--shadow-color);
}

.card-header {
    background-color: var(--card-header-bg);
    border-bottom: 1px solid var(--border-color);
    color: var(--text-color);
}

.card-header[role="button"] {
    cursor: pointer;
    user-select: none;
}

.card-header[role="button"]:hover {
    background-color: var(--hover-bg);
}

.card-header .fw-medium {
    color: var(--text-color);
    font-size: 1rem;
}

.card-header .badge {
    font-size: 0.75rem;
    font-weight: 500;
    padding: 0.35em 0.65em;
}

.card-header .btn-link {
    text-decoration: none;
    color: var(--text-color);
    padding: 0;
}

.card-header .btn-link:hover {
    color: var(--primary-color);
}

.card-header .btn-group {
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

/* Collapse Arrow Animation */
.card-header [role="button"] i {
    transition: transform 0.2s ease-in-out;
    color: var(--text-color);
    transform: rotate(-90deg);
}

.card-header [role="button"]:hover i {
    color: var(--text-color);
}

.card-header [role="button"][aria-expanded="true"] i {
    transform: rotate(0deg);
}

/* Mapping Types List */
.mapping-types-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.25rem;
    min-width: 280px;
}

.mapping-type-item {
    display: flex;
    align-items: center;
    padding: 0.4rem 0.5rem;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    cursor: move;
    user-select: none;
    transition: all 0.2s ease;
    font-size: 0.9rem;
}

.mapping-type-item:hover {
    background-color: var(--hover-bg);
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.mapping-type-item.dragging {
    opacity: 0.5;
}

/* Node List Styles */
#nodeListSection .card-body {
    max-height: 200px;
    overflow-y: auto;
}

#mappingPropertiesSection .card-body {
    height: 100%;
    overflow-y: auto;
}

.node-list-item {
    display: flex;
    align-items: center;
    padding: 8px;
    margin-bottom: 4px;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.node-list-item:hover {
    background-color: var(--hover-bg);
}

.node-list-item.selected {
    background-color: var(--selected-bg);
    border: 1px solid var(--selected-border);
}

.node-list-item i {
    margin-right: 8px;
    font-size: 1.1em;
}

.node-list-item span {
    margin-right: 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Delete Zone */
.delete-zone {
    position: absolute;
    right: -8.6rem;
    top: 50%;
    transform: translateY(-50%);
    width: 120px;
    height: 120px;
    background-color: var(--delete-zone-bg);
    border: 2px dashed var(--delete-zone-border);
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--delete-zone-border);
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
    z-index: 1000;
    backdrop-filter: blur(2px);
}

.delete-zone.visible {
    opacity: 1;
    pointer-events: all;
    box-shadow: 0 0 15px var(--delete-zone-shadow);
}

.delete-zone.drag-over {
    border-style: solid;
    transform: translateY(-50%) scale(1.05);
    background-color: var(--delete-zone-bg);
    box-shadow: 0 0 20px var(--delete-zone-shadow);
    cursor: grab;
}

.delete-zone-content {
    text-align: center;
    font-size: 0.9rem;
    font-weight: 500;
}

.delete-zone-content i {
    font-size: 2.2rem;
    display: block;
    margin-bottom: 0.5rem;
    filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.2));
}

/* Right Section Container */
.right-section-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.right-section-container > .card {
    min-width: 323px;
    margin-bottom: 0;
}

/* Remove Bootstrap's default margin adjustments */
.input-group > * {
    margin: 0 !important;
}

/* Form Control Styles */
.form-control {
    background-color: var(--card-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

.form-control:focus,
.form-control:active,
.form-control.listening {
    background-color: var(--card-bg) !important;
    color: var(--text-color) !important;
    border-color: var(--primary-color);
}

.form-control:read-only {
    background-color: var(--card-bg);
    cursor: pointer;
}

/* Input specific styles */
input[type="text"],
input[type="number"] {
    background-color: var(--card-bg);
    color: var(--text-color);
    border-color: var(--border-color);
}

input[type="text"]:focus,
input[type="number"]:focus,
input[type="text"]:active,
input[type="number"]:active,
input[type="text"].listening,
input[type="number"].listening {
    background-color: var(--card-bg) !important;
    color: var(--text-color) !important;
    border-color: var(--primary-color);
}

/* Node Scale Input */
#nodeScale {
    width: 60px;
    padding: 4px;
    --bs-btn-focus-shadow-rgb: none;
    border-radius: 0;
    border-right: 0;
    border-color: var(--border-color);
}

#decreaseScale {
    border-color: var(--border-color);
    border-right: 0;
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}

#increaseScale {
    border-color: var(--border-color);
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
}

#nodeScale:hover,
#decreaseScale:hover,
#increaseScale:hover {
    background-color: var(--hover-bg);
    border-color: var(--border-color);
    color: var(--text-color);
}

#nodeScale:focus {
    box-shadow: none;
    border-color: var(--border-color);
}

/* Input Controls */
.switch-key-input {
    width: 120px;
    padding: 6px;
}

/* Footer */
.footer {
    height: 40px;
    line-height: 40px;
    background-color: var(--bg-color);
    border-top: 1px solid var(--border-color);
    text-align: center;
    flex-shrink: 0;
}

.footer a {
    color: var(--text-color);
    font-weight: 500;
}

.footer a:hover {
    color: var(--primary-color);
}

.footer .bi-heart-fill {
    display: inline-block;
    font-size: 0.9rem;
    margin: 0 0.2rem;
    color: var(--danger-color);
    animation: heartbeat 2s ease infinite;
}

/* Animations */
@keyframes transformScalePulse {
    0% { transform: translateY(-50%) scale(1); }
    50% { transform: translateY(-50%) scale(1.05); }
    100% { transform: translateY(-50%) scale(1); }
}

@keyframes shadowPulse {
    0% { box-shadow: 0 0 0 0 rgba(66, 133, 244, 0.4); }
    70% { box-shadow: 0 0 0 0.3rem rgba(66, 133, 244, 0); }
    100% { box-shadow: 0 0 0 0 rgba(66, 133, 244, 0); }
}

@keyframes heartbeat {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); }
    100% { transform: scale(1); }
}

/* Key Input Styles */
.form-control.listening {
    background-color: #e8f0fe;
    border-color: #4285f4;
    box-shadow: 0 0 0 0.2rem rgba(66, 133, 244, 0.25);
    animation: shadowPulse 1.5s infinite;
}

/* Scrollbar Styles */
#mappingPropertiesSection .card-body::-webkit-scrollbar {
    width: 6px;
}

#mappingPropertiesSection .card-body::-webkit-scrollbar-track {
    background: #f1f1f1;
}

#mappingPropertiesSection .card-body::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
}

#mappingPropertiesSection .card-body::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}

/* Media Queries */
@media (max-width: 1200px) {
    body {
        overflow-y: auto;
    }

    .row {
        flex-direction: column;
        height: auto;
    }

    .col-md-9, .col-md-3 {
        width: 100%;
        max-width: 100%;
        flex: 0 0 100%;
    }

    #canvasContainer {
        height: 60vh;
        min-height: 400px;
        margin-bottom: 1rem;
    }

    .col-md-3 {
        min-width: 100%;
    }

    .card {
        margin-bottom: 1rem;
    }

    .delete-zone {
        position: fixed;
        right: 1rem;
        top: 50%;
    }

    .mapping-types-list {
        display: grid;
        grid-template-columns: 1fr;
    }

    /* Horizontal scroll for right sections */
    .right-section-container {
        flex-direction: row;
        overflow-x: auto;
        padding: 0.5rem;
        gap: 1rem;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
    }

    .right-section-container > .card {
        flex: 0 0 350px;
        scroll-snap-align: start;
        display: flex;
        flex-direction: column;
    }

    /* Show scrollbar */
    .right-section-container::-webkit-scrollbar {
        height: 8px;
    }

    .right-section-container::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 4px;
    }

    .right-section-container::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 4px;
    }

    .right-section-container::-webkit-scrollbar-thumb:hover {
        background: #555;
    }

    /* Adjust collapse behavior in horizontal layout */
    .right-section-container .collapse {
        display: flex;
        flex-direction: column;
        flex: 1;
    }

    .right-section-container .collapse:not(.show) {
        display: none;
    }

    /* Increase section heights in stacked layout */
    #availableMappings .card-body,
    #nodeListSection .card-body,
    #mappingPropertiesSection .card-body {
        height: auto;
        max-height: 400px;
    }

    .col-md-3 {
        overflow: hidden;
    }
}

@media (max-width: 920px) {
    .navbar {
        height: auto;
        padding: 0.5rem;
    }

    .navbar .btn-group {
        flex-wrap: wrap;
        justify-content: flex-start;
        gap: 0.5rem;
        margin: 0.5rem 0;
    }

    .navbar .btn-group .btn {
        flex: 1;
        min-width: 120px;
        white-space: nowrap;
    }
}

@media (max-width: 768px) {
    .delete-zone {
        width: 80px;
        height: 80px;
        right: 0.5rem;
    }
    
    .delete-zone-content {
        font-size: 0.75rem;
        padding: 0.25rem;
    }
    
    .delete-zone-content i {
        font-size: 1.8rem;
        margin-bottom: 0.3rem;
    }

    .delete-zone span {
        display: none;
    }
}

@media (max-width: 576px) {
    .navbar .brand-text {
        font-size: 0.9rem;
    }

    .navbar small {
        display: none;
    }

    #canvasContainer {
        height: 50vh;
        min-height: 300px;
    }
}

@media (max-height: 600px) {
    .delete-zone {
        top: auto;
        bottom: 1rem;
        transform: none;
    }
}

/* Theme Toggle */
#themeToggle {
    width: 38px;
    height: 38px;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#themeToggle i {
    font-size: 1.2rem;
}

/* Language Selector Styles */
#languageSelector {
    position: relative;
}

#languageSelector .dropdown-menu {
    position: absolute;
    z-index: 1000;
    right: 0;
    margin-top: 2px;
    min-width: 120px;
    padding: 0 !important;
    border-radius: 0.375rem;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
}

#languageSelector .dropdown-item {
    padding: 0.5rem 1rem;
    transition: all 0.2s ease;
}

#languageSelector .dropdown-item:hover {
    background-color: var(--hover-bg);
    transform: translateX(2px);
}

#languageSelector .dropdown-item.active {
    background-color: var(--selected-bg);
    color: var(--text-color);
}

#languageSelector .flag-icon {
    font-size: 1.2em;
    margin-right: 0.75rem;
    display: inline-block;
    vertical-align: middle;
    width: 1.5em;
    height: 1em;
    margin-top: -2px;
}

[dir="rtl"] #languageSelector .flag-icon {
    margin-right: 0;
    margin-left: 0.75rem;
}